import java.net.*;
import java.util.Vector;
import java.io.*;

public class principal
{

	public static inregistrare inr;
	public static client cl;  
	public static punctajfinal pf; 
	public static Socket socket = null;
	public static PrintWriter out = null;
	public static BufferedReader in = null;
	public static Vector sir=new Vector();
	public static int curent;
	
	public static boolean este_in_sir(int nr)
	{
		for(int i=0;i<sir.size();i++)
			if(nr==(Integer)sir.get(i))
				return true;
		return false;
	}
	
	public static void inregistreaza(String nume, String prenume, String CNP)
	{
		listenSocket();
		scrieServer("reg#nume="+nume+"&prenume="+prenume+"&cnp="+CNP);
		System.out.println("reg#nume="+nume+"&prenume="+prenume+"&cnp="+CNP);
		inr.setVisible(false);
		inr.dispose();
		cl=new client(nume+" "+prenume);
		cl.setVisible(true);
		//cl.setSize(new Dimension(9, 29));
		intrebare(1);
	}
	
	public static void intrebare(int nr)
	{
		scrieServer("intrreq#nrintr="+nr);
		cl.newQuestion(citesteServer());
	}
	
	public static String numarpuncte()
	{
		scrieServer("nrpct#");
		String str=citesteServer();
		if(str.split("#")[0].equals("nrpct"))
			return (str.split("#")[1]).split("=")[1];
		return "0";
	}
	
	public static void numarpunctefinal()
	{
		scrieServer("nrpctfin#");
		String str=citesteServer();
		if(str.split("#")[0].equals("nrpctfin"))
		{
			cl.setVisible(false);
			cl.dispose();
			closeSocket();
			pf=new punctajfinal();
			pf.setVisible(true);
			//pf.setSize(new Dimension(8, 11));
			pf.punctaj((str.split("#")[1]).split("=")[1]);
		}
		
	}
	
	public static void raspuns(int val,int nr)
	{
		scrieServer("rasp#rasp="+ val +"&nrintr="+nr);
	}
	
	public static void scrieServer(String str)
	{
		out.println(str);
		out.flush();
	}
	
	public static String citesteServer()
	{
		try
		{
			return in.readLine(); 
		}
		catch (Exception e)
		{
			System.out.println("Eroare: " + e.getMessage() );
		}
		return null;
	}
	
	public static void closeSocket()
	{
		try
		{
			in.close();
			out.close();
			socket.close();
		}
		catch(Exception e)
		{
			System.out.println("Eroare: "+e.getMessage());
		}
	}
	
	public static void listenSocket()
	{
		//Create socket connection
		try
	    {
			socket = new Socket("127.0.0.1", 4444);
			out = new PrintWriter(socket.getOutputStream(), true);
		    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
	    }
		catch (UnknownHostException e)
		{
			System.out.println("Unknown host: 127.0.0.1");
		    System.exit(1);
		}
		catch (IOException e)
		{
			System.out.println("No I/O");
		    System.exit(1);
		}
	}


	public static void main(String[] args)
	{
		inr=new inregistrare();
		inr.setVisible(true);
	}

}
